package com.iwhalecloud.citybrain.flow.platform.manager.application.listener;

import java.util.Date;
import java.util.Objects;

import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.SpringUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowMultiInstanceTaskTraceVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowMultiInstanceTaskTraceRepository;

/**
 * @author lich
 * @date 2021/11/2
 */
public abstract class MultiInstanceSingleCompleteListener {

    private static final long serialVersionUID = -8528731073758826331L;

    protected static final String NUMBER_OF_PASS_INSTANCES = "nrOfPassInstances";

    protected static final String NUMBER_OF_UNPASS_INSTANCES = "nrOfUnpassInstances";

    protected static final String APPROVAL = "approval";

    protected static final String PASS = "pass";


    protected void updateMultiInstanceTaskTrace(String approval, int newCount, String rootExecutionId) {
        FlowMultiInstanceTaskTraceRepository multiInstanceTaskTraceService = SpringUtils.getBean(FlowMultiInstanceTaskTraceRepository.class);
        FlowMultiInstanceTaskTraceVo multiInstanceTaskTrace = multiInstanceTaskTraceService.getByRootExecutionId(rootExecutionId);
        if (Objects.nonNull(multiInstanceTaskTrace)) {
            if (Objects.equals(PASS, approval)) {
                multiInstanceTaskTrace.setNumOfPassInstances(newCount);
            } else {
                multiInstanceTaskTrace.setNumOfUnpassInstances(newCount);
            }
            multiInstanceTaskTrace.setUpdateTime(new Date());
            multiInstanceTaskTraceService.updateMultiInstanceTaskTrace(multiInstanceTaskTrace);
        }
    }

}
